﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TMDT.BusinessObjects;

namespace TMDT.BusinessLogic
{
    public class SanPhamManager
    {
        public bool Add(SanPham entity)
        {
               return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spSanPhamInsert",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDSanPham",
                                                                                             entity.IDSanPham),
                                                                            new SqlParameter("@IDLoaiSanPham",
                                                                                             entity.IDLoaiSanPham),
                                                                            new SqlParameter("@IDNguoiBan",
                                                                                             entity.IDNguoiBan),
                                                                            new SqlParameter("@IDNhaSanXuat",
                                                                                             entity.IDNhaSanXuat),
                                                                            new SqlParameter("@IsAn", entity.IsAn),
                                                                            new SqlParameter("@IsKhuyenMai",
                                                                                             entity.IsKhuyenMai),
                                                                            new SqlParameter("@NoiDungKhuyenMai",
                                                                                             entity.NoiDungKhuyenMai),
                                                                            new SqlParameter("@NgayTao",
                                                                                             entity.NgayTao),
                                                                            new SqlParameter("@SoLuotXem",
                                                                                             entity.SoLuotXem),
                                                                            new SqlParameter("@SoLuong",
                                                                                             entity.SoLuong),
                                                                            new SqlParameter("@ThoiGianBaoHanh",
                                                                                             entity.ThoiGianBaoHanh),
                                                                            new SqlParameter("@Gia",
                                                                                             entity.Gia),
                                                                            new SqlParameter("@TenSanPham",
                                                                                             entity.TenSanPham),
                                                                        }
                                                    });
        }

        public bool Update(SanPham entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spSanPhamUpdate",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDSanPham",
                                                                                             entity.IDSanPham),
                                                                            new SqlParameter("@IDLoaiSanPham",
                                                                                             entity.IDLoaiSanPham),
                                                                            new SqlParameter("@IDNguoiBan",
                                                                                             entity.IDNguoiBan),
                                                                            new SqlParameter("@IDNhaSanXuat",
                                                                                             entity.IDNhaSanXuat),
                                                                            new SqlParameter("@IsAn", entity.IsAn),
                                                                            new SqlParameter("@IsKhuyenMai",
                                                                                             entity.IsKhuyenMai),
                                                                            new SqlParameter("@NoiDungKhuyenMai",
                                                                                             entity.NoiDungKhuyenMai),
                                                                            new SqlParameter("@NgayTao",
                                                                                             entity.NgayTao),
                                                                            new SqlParameter("@SoLuotXem",
                                                                                             entity.SoLuotXem),
                                                                            new SqlParameter("@SoLuong",
                                                                                             entity.SoLuong),
                                                                            new SqlParameter("@ThoiGianBaoHanh",
                                                                                             entity.ThoiGianBaoHanh),
                                                                            new SqlParameter("@Gia",
                                                                                             entity.Gia),
                                                                            new SqlParameter("@TenSanPham",
                                                                                             entity.TenSanPham),
                                                                        }
                                                    });
        }

        public bool Remove(Guid id)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spSanPhamDelete",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDSanPham", id)
                                                                        }
                                                    });
        }

        public List<SanPham> GetSanPham()
        {
            List<SanPham> sanPham = new List<SanPham>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from SanPham",
                                                         CommandType = CommandType.Text,
                                                     });
            foreach(DataRow row in dtSet.Tables[0].Rows)
            {
                sanPham.Add(SanPham.Convert(row));
            }
            return sanPham;
        }

        public SanPham GetSanPham(object id)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from SanPham where IDSanPham = @IDSanPham",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDSanPham", id),
                                                                         }
                                                     });

            return SanPham.Convert(dtSet.Tables[0].Rows[0]);
        }

        public List<SanPham> GetSanPhamByIDNguoiBan(Guid idNguoiBan)
        {
            List<SanPham> sanPham = new List<SanPham>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from SanPham where IDNguoiBan = @IDNguoiBan",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDNguoiBan", idNguoiBan)
                                                                             ,
                                                                         }
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                sanPham.Add(SanPham.Convert(row));
            }
            return sanPham;
        }
    }
}
